clc;
clear;

cd 'C:\home\Project\mylab\distmesh';

% fd = @(x) sqrt(sum(x.^2,2))-1;
% [node, elem] = distmesh2d(fd, @huniform, 0.5, [-1 -1;1 1], [])
% showmesh(node, elem);

% hlead=0.01;
% htrail=0.04;
% hmax=2;
% circx=2;
% circr=4;
% a=.12/.2*[0.2969,-0.1260,-0.3516,0.2843,-0.1036];
% fd=@(p) ddiff(dcircle(p,circx,0,circr),(abs(p(:,2))-polyval([a(5:-1:2),0],p(:,1))).^2-a(1)^2*p(:,1));
% fh=@(p) min(min(hlead+0.3*dcircle(p,0,0,0),htrail+0.3*dcircle(p,1,0,0)),hmax);
% fixx=1-htrail*cumsum(1.3.^(0:4)');
% fixy=a(1)*sqrt(fixx)+polyval([a(5:-1:2),0],fixx);
% fix=[[circx+[-1,1,0,0]*circr; 0,0,circr*[-1,1]]'; 0,0; 1,0; fixx,fixy; fixx,-fixy];
% box=[circx-circr,-circr; circx+circr,circr];
% h0=min([hlead,htrail,hmax]);
% [node, elem]=distmesh2d(fd,fh,h0,box,fix);
% showmesh(node, elem);

fd = @(p) ddiff(drectangle(p,-1,1,-1,1),dcircle(p,0,0,0.5));
fh = @(p) 0.05+0.3*dcircle(p,0,0,0.5);
[p,t] = distmesh2d(fd,fh,0.1,[-1,-1;1,1],[-1,-1;1,-1;1,1;-1,1]);
showmesh(p,t);
edge = boundedges(p,t);
[t1, t2] = mkt2t(t)


%% showmesh
function showmesh(node, elem)
Np = size(node,1);
Ne = size(elem,1);
figure;
% mesh
h = patch('Faces', elem, 'Vertices', node);
set(h, 'facecolor', 'w', 'edgecolor', 'k');
axis equal;
axis tight;
hold on;
% elem
center = zeros(Ne,2);
for i = 1:Ne
    V = node(elem(i,:),:);
    center(i,:) = mean(V);
end
text(center(:,1), center(:,2), int2str((1:Ne)'), 'FontSize', 10, 'FontWeight', 'bold', 'Color', 'r');
% node
shift = 0.001;
range = (1:Np)';
plot(node(range,1), node(range,2), 'k.', 'MarkerSize', 10);
text(node(range,1)+shift, node(range,2)+shift, int2str(range), 'FontSize', 10, 'FontWeight', 'bold', 'Color', 'k');
hold off
end